package com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.util.date.DateUtils;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.common.TencentCloudImUtil;
import com.moxiao.yfjh.module.maker.common.GenMakerNoCommonUtil;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoExcelVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoRespVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoUpdateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingExportReqVO;
import com.moxiao.yfjh.module.maker.convert.matchmakerInfo.MatchmakerInfoConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchMakerStringing.MatchmakerStringingDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchmakerInfo.MatchmakerInfoDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.memberStringing.MatchmakerMemberStringingDO;
import com.moxiao.yfjh.module.maker.service.matchMakerStringing.MatchmakerStringingService;
import com.moxiao.yfjh.module.maker.service.matchmakerInfo.MatchmakerInfoService;
import com.moxiao.yfjh.module.maker.service.memberStringing.MatchmakerMemberStringingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲红娘信息")
@RestController
@RequestMapping("/maker/matchmaker-info")
@Validated
public class MatchmakerInfoController {

    @Resource
    private MatchmakerInfoService matchmakerInfoService;
    @Resource
    private MatchmakerMemberStringingService matchmakerMemberStringingService;
    @Resource
    private MatchmakerStringingService matchmakerStringingService;
    @Resource
    private GenMakerNoCommonUtil genMakerNoCommonUtil;
    @Resource
    private TencentCloudImUtil tencentCloudImUtil;

    @PostMapping("/create")
    @Operation(summary = "创建相亲红娘信息")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:create')")
    public CommonResult<Long> createMatchmakerInfo(@Valid @RequestBody MatchmakerInfoCreateReqVO createReqVO) {
        genMakerNoCommonUtil.addMatchmakerInfoNo(createReqVO);
        Long matchmakerInfo = matchmakerInfoService.createMatchmakerInfo(createReqVO);
        tencentCloudImUtil.accountImport(String.valueOf(matchmakerInfo), createReqVO.getName(), createReqVO.getMerchantCover());
        return success(matchmakerInfo);
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲红娘信息")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:update')")
    public CommonResult<Boolean> updateMatchmakerInfo(@Valid @RequestBody MatchmakerInfoUpdateReqVO updateReqVO) {
        MatchmakerInfoDO matchmakerInfo = matchmakerInfoService.getMatchmakerInfo(updateReqVO.getId());
        if (Objects.nonNull(updateReqVO.getName()) && !Objects.equals(updateReqVO.getName(),matchmakerInfo.getName())
                    || Objects.nonNull(updateReqVO.getMerchantCover()) && !Objects.equals(updateReqVO.getMerchantCover(),matchmakerInfo.getMerchantCover())){
            tencentCloudImUtil.portraitSet(String.valueOf(getLoginUserId()),updateReqVO.getName(),null);
        }
        matchmakerInfoService.updateMatchmakerInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲红娘信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:delete')")
    public CommonResult<Boolean> deleteMatchmakerInfo(@RequestParam("id") Long id) {
        matchmakerInfoService.deleteMatchmakerInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲红娘信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:query')")
    public CommonResult<MatchmakerInfoRespVO> getMatchmakerInfo(@RequestParam("id") Long id) {
        MatchmakerInfoDO matchmakerInfo = matchmakerInfoService.getMatchmakerInfo(id);
        MatchmakerInfoRespVO vo = MatchmakerInfoConvert.INSTANCE.convert(matchmakerInfo);
        if (Objects.nonNull(vo)) {
            // 会员总数
            MatchmakerMemberStringingExportReqVO memberQuery = new MatchmakerMemberStringingExportReqVO();
            memberQuery.setMatchmakerId(vo.getId());
            List<MatchmakerMemberStringingDO> list = new ArrayList<>();
            list = matchmakerMemberStringingService.getMatchmakerMemberStringingList(memberQuery);
            vo.setMemberNum(list.size());
            // 本月会员总数
            MatchmakerMemberStringingExportReqVO memberQueryMonth = new MatchmakerMemberStringingExportReqVO();
            memberQueryMonth.setMatchmakerId(vo.getId());
            LocalDateTime[] localDateTimes = new LocalDateTime[2];
            // 往数组中添加数据
            localDateTimes[0] = DateUtils.getFirstMonth();
            localDateTimes[1] = DateUtils.getLastMonth();
            memberQueryMonth.setCreateTime(localDateTimes);
            List<MatchmakerMemberStringingDO> listMonth = new ArrayList<>();
            listMonth = matchmakerMemberStringingService.getMatchmakerMemberStringingList(memberQueryMonth);
            vo.setMonthMemberNum(listMonth.size());
            // 牵线总数 只查询牵线成功的
            List<MatchmakerStringingDO> stringingList = new ArrayList<>();
            MatchmakerStringingExportReqVO exportReqVO = new MatchmakerStringingExportReqVO();
            exportReqVO.setMatchmakerId(vo.getId());
            exportReqVO.setStringingStatus((byte) 1);
            stringingList = matchmakerStringingService.getMatchmakerStringingList(exportReqVO);
            vo.setStringingTotalNum(stringingList.size());
            // 本月牵线数
            List<MatchmakerStringingDO> stringingListMonth = new ArrayList<>();
            MatchmakerStringingExportReqVO exportReqMonth = new MatchmakerStringingExportReqVO();
            exportReqMonth.setStringingStatus((byte) 1);
            exportReqMonth.setMatchmakerId(vo.getId());
            // 本月第一天 -本月最后一天
            LocalDateTime[] stringingTimes = new LocalDateTime[2];
            stringingTimes[0] = DateUtils.getFirstMonth();
            stringingTimes[1] = DateUtils.getLastMonth();
            exportReqMonth.setCreateTime(stringingTimes);


            stringingListMonth = matchmakerStringingService.getMatchmakerStringingList(exportReqMonth);
            vo.setStringingMonthNum(stringingListMonth.size());
        }


        return success(vo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲红娘信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:query')")
    public CommonResult<List<MatchmakerInfoRespVO>> getMatchmakerInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<MatchmakerInfoDO> list = matchmakerInfoService.getMatchmakerInfoList(ids);
        return success(MatchmakerInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲红娘信息分页")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:query')")
    public CommonResult<PageResult<MatchmakerInfoRespVO>> getMatchmakerInfoPage(@Valid MatchmakerInfoPageReqVO pageVO) {
        PageResult<MatchmakerInfoDO> pageResult = matchmakerInfoService.getMatchmakerInfoPage(pageVO);
        PageResult<MatchmakerInfoRespVO> page = MatchmakerInfoConvert.INSTANCE.convertPage(pageResult);
        page.getList().forEach(item -> {
            // 会员总数
            MatchmakerMemberStringingExportReqVO memberQuery = new MatchmakerMemberStringingExportReqVO();
            memberQuery.setMatchmakerId(item.getId());
            List<MatchmakerMemberStringingDO> list = new ArrayList<>();
            list = matchmakerMemberStringingService.getMatchmakerMemberStringingList(memberQuery);
            item.setMemberNum(list.size());
            // 本月会员总数
            MatchmakerMemberStringingExportReqVO memberQueryMonth = new MatchmakerMemberStringingExportReqVO();
            memberQueryMonth.setMatchmakerId(item.getId());
            LocalDateTime[] localDateTimes = new LocalDateTime[2];
            // 往数组中添加数据
            localDateTimes[0] = DateUtils.getFirstMonth();
            localDateTimes[1] = DateUtils.getLastMonth();
            memberQueryMonth.setCreateTime(localDateTimes);
            List<MatchmakerMemberStringingDO> listMonth = new ArrayList<>();
            listMonth = matchmakerMemberStringingService.getMatchmakerMemberStringingList(memberQueryMonth);
            item.setMonthMemberNum(listMonth.size());
            // 牵线总数 只查询牵线成功的
            List<MatchmakerStringingDO> stringingList = new ArrayList<>();
            MatchmakerStringingExportReqVO exportReqVO = new MatchmakerStringingExportReqVO();
            exportReqVO.setStringingStatus((byte) 1);
            exportReqVO.setMatchmakerId(item.getId());

            stringingList = matchmakerStringingService.getMatchmakerStringingList(exportReqVO);
            item.setStringingTotalNum(stringingList.size());
            // 本月牵线数
            List<MatchmakerStringingDO> stringingListMonth = new ArrayList<>();
            MatchmakerStringingExportReqVO exportReqMonth = new MatchmakerStringingExportReqVO();
            exportReqMonth.setMatchmakerId(item.getId());
            exportReqMonth.setStringingStatus((byte) 1);
            // 本月第一天 -本月最后一天
            LocalDateTime[] stringingTimes = new LocalDateTime[2];
            stringingTimes[0] = DateUtils.getFirstMonth();
            stringingTimes[1] = DateUtils.getLastMonth();
            exportReqMonth.setCreateTime(stringingTimes);
            stringingListMonth = matchmakerStringingService.getMatchmakerStringingList(exportReqMonth);
            item.setStringingMonthNum(stringingListMonth.size());
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲红娘信息 Excel")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerList:export')")
    @OperateLog(type = EXPORT)
    public void exportMatchmakerInfoExcel(@Valid MatchmakerInfoExportReqVO exportReqVO,
                                          HttpServletResponse response) throws IOException {
        List<MatchmakerInfoDO> list = matchmakerInfoService.getMatchmakerInfoList(exportReqVO);
        // 导出 Excel
        List<MatchmakerInfoExcelVO> datas = MatchmakerInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲红娘信息.xls", "数据", MatchmakerInfoExcelVO.class, datas);
    }

}
